package bookSystem.operations;

import bookSystem.book.Book;
import bookSystem.book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    //删除图书
    public void work(BookList bookList){
        System.out.println("删除图书！");

        //1、找到你要删除的书
        int size = bookList.getUsedSize();
        if(size == 0) {
            System.out.println("书架为空，不能删除！");
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名：");
        String name = scanner.nextLine();
        int index = -1;
        for (int i = 0; i < size; i++) {
            Book book1 = bookList.getBook(i);
            if(book1.getName().equals(name)) {
                index = i;
                break;
            }
        }
        //2、index ！= -1  有这本书 开始删除
        if(index == -1) {
            System.out.println("没有你要删除的书！");
            return;
        }
        for (int i = index; i < size-1; i++) {
            Book book1 = bookList.getBook(i+1);
            bookList.setBooks(i,book1);
        }
        //当书放删掉之后 需要维持usedSize
        bookList.setUsedSize(size-1);
    }
}
